import { defineStore } from 'pinia'
import { ref } from 'vue'
import { editUserInfo, getUserInfo } from '@/api/user.js'

export const useUserStore = defineStore('user', () => {
  const token = ref('')
  const user = ref({})
  const setToken = (newToken) => {
    token.value = newToken
  }
  const getUser = async () => {
    // 这么写可以完成链式编程，但是有点丑
    // user.value = (await getUserInfo()).data.data
    const res = await getUserInfo()
    user.value = res.data.data
  }
  const editUser = async (data) => {
    await editUserInfo(data)
  }
  const removeUser  =  () => {
    user.value = {}
  }
  const removeToken = () => {
    token.value = undefined
  }
  return {
    token,
    setToken,
    removeToken,
    user,
    getUser,
    removeUser,
    editUser
  }
},
  {
    persist: true
  })
